﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Wuhong.AppDomainTest;
using 应用程序域动态库１;

namespace 应用程序域
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            System.Threading.Tasks.TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;


            //这个由于应用程序域的原因没有运行完
            Loaded += MainWindow_Loaded;




        }

        void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            e.SetObserved();
            Console.WriteLine(e.Exception + string.Empty);

        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {


            Loaded -= MainWindow_Loaded;
            //启用影像复制程序集
            //本地 AppDomain程序集也能删除
            AppDomain.CurrentDomain.SetupInformation.ShadowCopyFiles = "true";


            //应用程序域测试代码
            string funame = typeof(DemoClass).Assembly.FullName;
            DemoClass a = new DemoClass();
            AppDoTest test = new AppDoTest();
            AppDomain tmp = test.CreateAppDomain("sfsdfdsfsd");

            tmp.Load(funame);
            Console.WriteLine("加载dll完成{0}", funame);
            #region 不可用
            var Iproxy = tmp.CreateInstance(funame, a.GetType().FullName);
            DemoClass demo = Iproxy.Unwrap() as DemoClass;
            // demo.SetThis();
            //设置对像%
            //tmp.SetData("abc", demo);
            //DemoClass t = tmp.GetData("demo") as DemoClass;
            var gx = demo.GetGuid;
            var list = demo.Ccc;
            demo.Obj = a;
            Console.WriteLine("应用程序域穿透.另外的应用程序域{0}", gx);

            Console.WriteLine("引起一个末处理的异常----不知道为什么程序暂停了...");


            //tmp.DoCallBack(CallBack);
            //demo.引起一个末处理的异常();



            #endregion
            #region 透明代理可用
            AppDomain appRef = AppDomain.CreateDomain("MarshalByRef");
            string assemblyName = Assembly.GetEntryAssembly().FullName;
            //按引用封送
            MBR mbr = (MBR)appRef.CreateInstanceAndUnwrap(assemblyName, typeof(MBR).FullName);
            Guid g = mbr.GetGuid;
            Console.WriteLine("执行数据功能{0}", funame);
            #endregion

            ///另一个代理测试
            //Wuhong.AppDomainTest.MainPoint.Do();
            Console.WriteLine("Load完成....");

            // Console.BackgroundColor = ConsoleColor.DarkYellow;
            Console.ForegroundColor = ConsoleColor.Green;
        }

        /// <summary>
        /// 边界访问
        /// </summary>
        ProxyAppDomain proxyApp = new ProxyAppDomain();

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //Wuhong.AppDomainTest.MainPoint.Do();



            System.Threading.Tasks.Task thr = new System.Threading.Tasks.Task(Run一个新应用程序域);

            thr.Start();

            // thr.Wait();

            //System.Threading.Tasks.Task
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("点击按钮完成....");
            Console.ForegroundColor = ConsoleColor.Green;
        }




        void Run一个新应用程序域()
        {

            string funame = typeof(DemoClass).Assembly.FullName;
            DemoClass a = new DemoClass();
            AppDoTest test = new AppDoTest();
            AppDomain tmp = test.CreateAppDomain("sfsdfdsfsd");

            tmp.Load(funame);
            Console.WriteLine("加载dll完成{0}", funame);

            var Iproxy = tmp.CreateInstance(funame, a.GetType().FullName);
            DemoClass demo = Iproxy.Unwrap() as DemoClass;
            // demo.SetThis();
            //设置对像%
            //tmp.SetData("abc", demo);
            //DemoClass t = tmp.GetData("demo") as DemoClass;
            var gx = demo.GetGuid;
            var list = demo.Ccc;
            demo.Obj = a;
            Console.WriteLine("应用程序域穿透.另外的应用程序域{0}", gx);

            Console.WriteLine("引起一个末处理的异常----不会程序暂停了...");

            //当前应用程序域异常处理
            tmp.DoCallBack(proxyApp.CallBack);


            demo.加入自己的异常处理();

            //实际上调用的是当前应用程序域
            tmp.DoCallBack(proxyApp.Error);


            //demo.引起一个末处理的异常();


        }
    }



}
